defaultSpeed = 5;
textSearchStartFontSize = 40;
textSearchStopFontSize =20; 
textSearchStartX = 70;
textSearchStartY = 200;
textSearchStopX = 10;
textSearchStopY = 25;
font = 'Arial';
function textToCorner(pageState,text){
	a = new AText([text],textSearchStartX, textSearchStartY,"", textSearchStartFontSize,font,-1);
	act = [];

	act.push(new TextMoveAction(a,textSearchStopX, textSearchStopY,defaultSpeed));
	act.push(new FontSizeAction(a,textSearchStopFontSize,defaultSpeed));

	and = new AndAction(act);
	a.addAction(and);
	l = new Line(textSearchStopX,textSearchStopY+10,textSearchStopX,
        textSearchStopY+10,'black',5);
	l.addAction(new LineMoveAction(textSearchStopX,textSearchStopY+10,
            400+textSearchStopX,textSearchStopY+10,defaultSpeed,l));
	


	 var ret = new DrawStage([a,l]);
	 var stagearr=[new FillColor(new Color(0,0,0)),a,l, 
                new ChangeColor(new Color(255,255,255),new Color(0,0,0),5)]
	 stagearr = stagearr.concat(renderQueryAnswer(pageState,text))
	 ret.next = new DrawStage(stagearr)
	 return ret;
}

function createResponseAText(bookname,chapterno,verseno,text,x,y,fontstyle,
        fontsize,fontstr,width){
    var arrx = [bookname+" "+chapterno+":"+verseno];
	var one = new AText(arrx,x,y,"bold",fontsize,fontstr,width);
	var two = new AText([text],x,y+one.height(),fontstyle,fontsize,fontstr,width);
    return [one,two]
}

function renderQueryAnswer(pageState,query){
	 var xmlhttp=new XMLHttpRequest()
  var reqStr = "/querybible/query?"+"json="+JSON.stringify({'query':query})

  xmlhttp.open("GET",reqStr,false);
  xmlhttp.send();
	response = JSON.parse(xmlhttp.responseText)
	if(response.length > 0){
		a = createResponseAText(response[0].bookname,response[0].chapterNum,
            response[0].verseNum,response[0].verse,textSearchStopX, 
            textSearchStopY+30,"", 18,font,1200);
		var arr = a
        var i
		for(  i = 1; i < response.length; i++){
        	var as  = createResponseAText(response[i].bookname,response[i].chapterNum,
                        response[i].verseNum,response[i].verse,textSearchStopX, 
                        arr[arr.length-1].y+arr[arr.length-1].height()+10,"", 18,font,1200)
            var j;
            for( j = 0; j < as.length;j++){
                arr.push(as[j])
            }

		 }
//new AText(response[i].bookname+": "
//                +response[i].verse,textSearchStopX, 
//                arr[i-1].y+arr[i-1].height()+10,"", 18,font,1200));
		return arr;
	}else{
		return []
	}
}
function TextSearchContext(canvas,startText){
	this.pageState= getPageState(canvas);
	var ref = this;
	this.text = startText;
	this.enabled = true;
	this.textSearch = function (){
		enable();
	}
	function enable(){
		ref.enabled = true;
		document['onkeydown'] = textSearchKeyPressed;
	}
	function disable(){

		document['onkeydown'] = nothing;
		ref.enabled = false;
	}
	function nothing(){}
	this.i = 0;
	function textSearchKeyPressed(e){
		
		var evt = e || window.event;
		ref.i = ref.i + 1;
		if(evt.keyCode == 13){
			
			if(ref.enabled == true){
				ref.pageState.draw(textToCorner(ref.pageState,ref.text),nothing,disable);
			}
			
		}else{
			if(evt.keyCode == 8){
				ref.text = ref.text.slice(0,ref.text.length-1);
			}else{
				ref.text+=String.fromCharCode(evt.keyCode);
			}
            
			a = new AText([ref.text],textSearchStartX,textSearchStartY,"",
                textSearchStartFontSize,font,-1);
			if(ref.enabled == true){
				ref.pageState.draw(new DrawStage([a]),enable,disable);
			}
		}
	}
}

function ResultsContext(canvas,resultsText ){
	this.pageState= getPageState(canvas);
	var ref = this;
	this.text = startText;
	this.enabled = true;
	this.textSearch = function (){
		enable();
	}
	function enable(){
		ref.enabled = true;
		document['onkeydown'] = textSearchKeyPressed;
	}
	function disable(){

		document['onkeydown'] = nothing;
		ref.enabled = false;
	}
	function nothing(){}
	this.i = 0;
	function textSearchKeyPressed(e){
		
		var evt = e || window.event;
		ref.i = ref.i + 1;
		if(evt.keyCode == 13){
			
			if(ref.enabled == true){
				ref.pageState.draw(textToCorner(ref.pageState,ref.text),nothing,disable);
			}
			
		}else{
			if(evt.keyCode == 8){
				ref.text = ref.text.slice(0,ref.text.length-1);
			}else{
				ref.text+=String.fromCharCode(evt.keyCode);
			}
			a = new AText([ref.text],textSearchStartX,textSearchStartY,"",
                textSearchStartFontSize,font,-1);
			if(ref.enabled == true){
				ref.pageState.draw(new DrawStage([a]),enable,disable);
			}
		}
	}
}
//function draw_b() {
//  var xmlhttp=new XMLHttpRequest()
//  var reqStr = "/bible/verse?book="
//  reqStr = reqStr + parseInt(1);
//  reqStr = reqStr+ "&chapter=1&verse=1";
//
//  var ol = "/bible/verse?book=1&chapter=1&verse=1";
//  xmlhttp.open("GET",reqStr,false);
//  xmlhttp.send();
//  var b_canvas = document.getElementById("a");
//  var b_context = b_canvas.getContext("2d");
//  a.text=xmlhttp.responseText;
//}
